package com.google.gsaguide.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gsaguide.MainActivity;
import com.google.gsaguide.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * This class contains the NavigationDrawer list adapter.
 * @author Felipe Porge Xavier - http://www.felipeporge.com
 */
public class NavigationDrawerAdapter extends BaseAdapter {

    private ArrayList<HashMap<String, String>> mArrayList;
    private Context mContext;

    public NavigationDrawerAdapter(Context context, ArrayList<HashMap<String, String>> arrayData) {
        this.mArrayList = arrayData;
        this.mContext = context;
    }

    public int getCount() {
        return mArrayList.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }


    public View getView(final int position, View vi, ViewGroup parent) {

        // Get the current HashMap.
        HashMap<String, String> hashData = mArrayList.get(position);

        // If the type equals "section", inflate a new section item.
        if(hashData.get("type").equals("section")){
            // Inflate new "section".
            if (vi == null)
                vi = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                        .inflate(R.layout.navigation_drawer_section, parent, false);

            // Set the section title.
            ((TextView) vi.findViewById(R.id.list_item_section_text)).setText(hashData.get("title"));

            // Disable all the view actions.
            vi.setOnClickListener(null);
            vi.setOnLongClickListener(null);
            vi.setLongClickable(false);

        }else if(hashData.get("type").equals("item")){
            // If the type equals "item", inflate a new list item.
            if (vi == null)
                vi = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                        .inflate(R.layout.navigation_drawer_item, parent, false);

            // Set the item title.
            ((TextView)vi.findViewById(R.id.title_section_view)).setText(hashData.get("title"));

            // Get the icon ID and show it in the imageView.
            int icoID = mContext.getResources().getIdentifier(hashData.get("ico"),
                    "drawable", MainActivity.class.getPackage().getName());
            ((ImageView)vi.findViewById(R.id.ico_section_view)).setImageDrawable(mContext.getResources().getDrawable(icoID));
        }

        return vi;
    }
}
